#include <cstring>
#include <iostream>
#include <algorithm>

#define x first
#define y second

using namespace std;

const int N = 1010;

typedef pair<int, int> PII;

int n, m;
char g[N][N];
PII q[N * N];
int dist[N][N];

void bfs() {
    int hh = 0, tt = -1;
    memset(dist, -1, sizeof dist);
    
    for (int i = 0; i < n; ++ i)
        for (int j = 0; j < m; ++ j)
            if (g[i][j] == '1') {
                q[ ++ tt] = {i, j};
                dist[i][j] = 0;
            }
    
    int dx[4] = {-1, 0, 1, 0}, dy[4] = {0, 1, 0, -1};    
    while (hh <= tt) {
        PII t = q[hh ++ ];
        
        for (int i = 0; i < 4; ++ i) {
            int a = t.x + dx[i], b = t.y + dy[i];
            if (a < 0 || a >= n || b < 0 || b >= m) continue;
            if (dist[a][b] != -1) continue ;
            
            dist[a][b] = dist[t.x][t.y] + 1;
            q[ ++ tt] = {a, b};
        }
    }
}

int main () {
    cin >> n >> m;
    
    for (int i = 0; i < n; ++ i) scanf("%s", g[i]);
    
    bfs();
    
    for (int i = 0; i < n; ++ i) {
        for (int j = 0; j < m; ++ j) 
            printf("%d ", dist[i][j]);
        puts("");
    }
    
    return 0;
}